package org.lib.servicehi.api;

import java.security.Principal;
import java.util.Date;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

/**
 * 权限测试接口
 * @author ben
 * @date 2021-10-21 21:15:06 CST
 */
@RestController
@RequestMapping(value="/hi")
@Slf4j
public class HiController {

	@GetMapping(value="/free")
	public String free() {
		return "free, " + new Date();
	}

	@PreAuthorize(value = "hasAuthority('ROLE_ADMIN')")
	@GetMapping(value="/admin")
	public String admin() {
		return "ADMIN, " + new Date();
	}

	@PreAuthorize(value = "hasAuthority('ROLE_USER')")
	@GetMapping(value="/user")
	public String user() {
		return "USER, " + new Date();
	}
	
	@GetMapping(value = "/getPrinciple")
	public OAuth2Authentication getPrinciple(OAuth2Authentication oauth2Authentication,
			Principal principal, Authentication authentication) {
		log.info("oauth2Authentication={}", oauth2Authentication);
		log.info("principal={}", principal);
		log.info("authentication={}", authentication);
		return oauth2Authentication;
	}
}
